package apollo.generator.common.filter;

import apollo.generator.common.interceptor.MyServletRequestWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <Description>
 * 替换Request对象
 * @author Jun Lee
 * @version 1.0
 * @createDate 2020/03/28 13:17
 */
@Component
public class RequestReplaceFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String contentType = request.getContentType();
        if (!(request instanceof MyServletRequestWrapper) && StringUtils.isNotEmpty(contentType) &&
                !contentType.contains("multipart/form-data")) {
            request = new MyServletRequestWrapper(request);
        }
        filterChain.doFilter(request, response);
    }
}